package com.itsxb.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.itsxb.constants.Constants;
import com.itsxb.constants.GlobalExceptionCode;
import com.itsxb.feign.LoginApi;
import com.itsxb.pojo.domain.Driver;
import com.itsxb.mapper.DriverMapper;
import com.itsxb.pojo.domain.DriverAuthMaterial;
import com.itsxb.pojo.dto.LoginDto;
import com.itsxb.pojo.dto.WechatRegisterDto;
import com.itsxb.result.R;
import com.itsxb.service.*;
import com.itsxb.template.WechatTemplate;
import com.itsxb.utils.AssertUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itsxb.utils.BitStatesUtil;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 司机对象 服务实现类
 * </p>
 *
 * @author lsy
 * @since 2023-12-16
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {


    @Autowired
    private WechatTemplate wechatTemplate;

    @Autowired
    private IDriverSettingService iDriverSettingService;

    @Autowired
    private IDriverSummaryService iDriverSummaryService;

    @Autowired
    private IDriverWalletService iDriverWalletService;
    @Autowired
    private IDriverAuthMaterialService driverAuthMaterialService;
    @Autowired
    private LoginApi loginApi;

    /**
     * 微信注册
     * 1.参数校验
     * 2.业务校验
     * 3.业务实现
     * 有些表数据是需要保存主表时，初始化出来的
     */
    @Override
    @GlobalTransactional(name = "driver-wechat-register", rollbackFor = Exception.class)
    public void wechatRegister(WechatRegisterDto wechatRegisterDto) {
        //业务逻辑层 三部分
        //1.JSR303校验
        //1.1 JSR303校验前端传给后端的值
        //2.业务校验
        //3.业务实现

        String openId = wechatTemplate.code2OpenId(wechatRegisterDto.getLoginCode());
        Driver driver = super.getOne(new LambdaQueryWrapper<Driver>().eq(Driver::getOpenId, openId));
        //2.2判断driver是否为空
        AssertUtil.isNull(driver, GlobalExceptionCode.PARAM_PHONE_EXIST);
        String phoneNumber = wechatTemplate.code2Phone(wechatRegisterDto.getPhoneCode());
        Long id = saveDriver(openId, phoneNumber);
        iDriverSettingService.initDriverSetting(id);
        iDriverSummaryService.initDriverSummary(id);
        iDriverWalletService.initDriverWallet(id);
        LoginDto loginDto = new LoginDto();
        loginDto.setId(id);
        loginDto.setType(Constants.Login.TYPE_DRIVER);
        loginDto.setOpenId(openId);
        loginDto.setPhone(phoneNumber);
        R r = loginApi.saveLogin(loginDto);
        AssertUtil.isTrue(r.isSuccess(), GlobalExceptionCode.WECHAT_REGISTER_ERROR);
//        int y = 1/0;
    }

    @Override
    public void realNameAuth(DriverAuthMaterial driverAuthMaterial) {
        //1.参数检验

        //2. 业务检验
        //2.1 校验driver_auth_material表中是否正在审核或者审核成功
        long loginIdAsLong = StpUtil.getLoginIdAsLong();
        List<DriverAuthMaterial> lists = driverAuthMaterialService.list(new LambdaQueryWrapper<DriverAuthMaterial>()
                .eq(DriverAuthMaterial::getDriverId, loginIdAsLong)
                .in(DriverAuthMaterial::getRealAuthStatus, Constants.AuditStatus.AUDITING, Constants.AuditStatus.AUDITED_SUCCESS));
        AssertUtil.isTrue(CollectionUtil.isEmpty(lists), GlobalExceptionCode.USER_AUDIT);

        //3.业务实现
        //3.1 保存到driver_auth_material表中
        //这里的逻辑是不对的，我不应该新创建一个DriverAuthMaterial类的对象 而是继续使用Controller层传过来的DriverAuthMaterial对象
        driverAuthMaterial.setDriverId(loginIdAsLong);
        driverAuthMaterial.setCreateTime(new Date());
        driverAuthMaterial.setRealAuthStatus(Constants.AuditStatus.AUDITING);
        driverAuthMaterialService.save(driverAuthMaterial);
        //3.2 修改司机表中的位状态 这里些错了
        Driver driver = getById(loginIdAsLong);
        //将查到的driver值 修改它的位状态
        driver.setBitState(BitStatesUtil.addState(driver.getBitState(), BitStatesUtil.OP_REAL_AUTHENTICATING));
        //修改driver
        updateById(driver);

    }


    /**
     * 3.1.保存司机表
     */
    private Long saveDriver(String openId, String phoneNumber) {
        Driver driver = new Driver();
        driver.setPhone(phoneNumber);
        driver.setCreateTime(new Date());
        driver.setOpenId(openId);
        driver.setBitState(0L);
        super.save(driver);
        return driver.getId();
    }
}